package com.kintreda.ieco_server.util.QRCode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.util.QRCode
 * @Author ZhongYao_H
 * @Date 2019/4/30:10:22
 * @Year 2019 Month 04 Day 30
 * @Date_zh 2019 年 四月 星期二
 */

public class QrCode {


//    public static void main(String[] args) {
//        String url = "http://www.baidu.com";
//        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
//        String fileName = "temp.jpg";
//        System.out.println("path = " + path);
//        createQrCode(url, path, fileName);
//    }

    public static String createQrCode(String url, String path, String fileName,String text) {
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file = new File(path, fileName);

            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
                writeToFile(bitMatrix, "jpg", file,text);
                System.out.println("搞定：" + file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    static void writeToFile(BitMatrix matrix, String format, File file,String text) throws IOException {
        BufferedImage image = toBufferedImage(matrix,text,file);
        ImageIcon imgIcon=new ImageIcon(file.getPath());
        Graphics2D g=image.createGraphics();
        Font font = new Font("黑体", Font.BOLD, 30);

        int width = matrix.getWidth();
        int height = matrix.getHeight();
        /** 设置字体在图片中的位置 在这里是居中* */
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(text, context);
        double x = (width - bounds.getWidth()-bounds.getWidth()) / 1.5;
        double y = (height - bounds.getHeight());
        double ascent = -bounds.getY();
        double baseY = y + ascent;

        g.setColor(Color.BLACK);
        g.setBackground(Color.WHITE);
        g.drawImage(image, 0, 0, width - 25, height - 25, null);
        g.setFont(new Font("宋体",Font.PLAIN,50)); //字体、字型、字号
        g.drawString(text,(int)x,(int)y); //画文字
        g.dispose();
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }

    static void writeToStream(BitMatrix matrix, String format, OutputStream stream,File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix,"",file);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private static BufferedImage toBufferedImage(BitMatrix matrix,String text,File file) {

        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
}